package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.MenuContent;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by wangjian7 on 16-7-20.
 */
public class MenuContentConvert {

    public static DBObject castMenuContentToDBObject(MenuContent menuContent) {
        DBObject dbo = new BasicDBObject();
        if (menuContent.getId() != null) {
            dbo.put("_id", menuContent.getId());
        }
        dbo.put("menuId", menuContent.getMenuId());
        dbo.put("programId", menuContent.getProgramId());
        dbo.put("pType", menuContent.getPType());
        dbo.put("priority", ObjectUtils.toLong(menuContent.getPriority(), 0L));
        dbo.put("startTime", menuContent.getStartTime());
        dbo.put("createTime", ObjectUtils.toLong(menuContent.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", System.currentTimeMillis());
        return dbo;
    }

    public static MenuContent castDBObjectToMenuContent(DBObject dbObject) {
        MenuContent menuContent = new MenuContent();
        if (dbObject != null) {
            menuContent.setId(ObjUtils.toLong(dbObject.get("_id")));
            menuContent.setMenuId(ObjUtils.toLong(dbObject.get("menuId")));
            menuContent.setProgramId(ObjUtils.toLong(dbObject.get("programId")));
            menuContent.setPType(ObjectUtils.toInteger(dbObject.get("pType")));
            menuContent.setPriority(ObjUtils.toLong(dbObject.get("priority")));
            menuContent.setStartTime(ObjUtils.toLong(dbObject.get("startTime")));
            menuContent.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            menuContent.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));
        } else {
            return null;
        }
        return menuContent;
    }
}
